package com.sxyjhh.service.data_exchange;

import com.sxyjhh.InitStartProc;
import com.sxyjhh.common.DBConfig;
import com.sxyjhh.create.api.DataApi;
import com.sxyjhh.dbutil.basic.ConstConfig;
import com.sxyjhh.dbutil.basic.DBQueryUtils;
import com.sxyjhh.dbutil.utils.CommonUtils;
import com.sxyjhh.dbutil.utils.DBInfoUtil;
import com.sxyjhh.schedule.TaskSchedulerManager;
import com.sxyjhh.utils.FtpUtil;
import com.sxyjhh.utils.IdGenerator;
import com.sxyjhh.utils.util.XaUtil;
import net.sf.json.JSONObject;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.log4j.Logger;
import org.apache.tools.ant.taskdefs.Sleep;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.sql.SQLException;
import java.util.*;
import java.util.concurrent.CountDownLatch;

/**
 * 数据推送服务类
 * Created by Administrator on 2018/7/20.
 */
@Service
public class PushService {


    private FTPClient ftpClient = new FTPClient();

    private Map<String, String> dicMap;

    private String dicTableName = DBConfig.readPop("DIC.TABNAME");

    @Autowired
    DBQueryUtils dbQueryUtils;

    /*数据表名*/
    private String safetyTabName = DBConfig.readPop("COMPANY.SAFETY.CERTIFICATE.OUT");
    private String personTabName = DBConfig.readPop("PERSON.SECURITY.CERTIFICATE.OUT");
    private String specialTabName = DBConfig.readPop("PERSON.SPECIAL.CERTIFICATE.OUT");
    private String imagesTabName = DBConfig.readPop("PERSON.IMAGES.OUT");

    /*推送错误数据库连接*/
    private String recordIp = DBConfig.readPop("output.record.ip");
    private String recordDbName = DBConfig.readPop("output.record.db.name");
    private String recordUserName = DBConfig.readPop("output.record.username");
    private String recordPassword = DBConfig.readPop("output.record.password");
    private String recordPort = DBConfig.readPop("output.record.port");
    private String recordDbSType = DBConfig.readPop("output.record.type");

    private static final Logger logger = Logger.getLogger(PushService.class);

    private int ZJNUM = 0;
    private int RYNUM = 0;
    private int TPNUM = 0;

    private Map<String, String> getDicMap() {
        if (XaUtil.isEmpty(dicMap)) {
            dicMap = new HashMap<>();

            DataApi ri = new DataApi(DBConfig.readPop("daq.task.pconnect.url"));
            List<String> dicList = null;
            try {
                dicList = ri.selectDataQ("select FNUMBER,FNAME from " + dicTableName);
            } catch (SQLException e) {
                e.printStackTrace();
            }

            if (!XaUtil.isEmpty(dicList)) {
                for (String dic : dicList) {
                    String[] dicArray = dic.split("&&");
                    dicMap.put(dicArray[0], dicArray[1]);
                }
            }
        }

        return dicMap;
    }

    @Autowired
    private TaskSchedulerManager taskSchedulerManager;


    /*
     * 1.获取比对后的数据
     * 2.将数据转化为标准推送数据
     * 3.通过多线程推送数据
     * */

    //推送安保人员
    public List<Map<String, Object>> pushRy(String tableName, List<Map<String, Object>> resList) {

        if (XaUtil.isEmpty(resList)) {
            return new ArrayList<>();
        }

        try {
            FtpUtil.getInstance().connect(ftpClient, "192.168.2.180", 2121, "yjhh", "a1b2c3d4!");
        } catch (IOException e) {
            errorRecord(e.getMessage(), "ftpClientInitError", personTabName);
            return new ArrayList<>();
        }

        Map<String, String> pathMap = new HashMap<>();

        int count = resList.size();
        int num = 1000;
        int batch = (count / num) + (count % num != 0 ? 1 : 0);
        for (int j = 0; j < batch; j++) {
            logger.info("关联图片查询批次");
            logger.info(j);
            StringBuilder sql = new StringBuilder();
            sql.append("SELECT  FLINKID as FID,FCONTENT as PATH from ");
            sql.append(imagesTabName);
            sql.append(" where FLINKID IN ( '");
            int endNum = (j + 1) * num;
            if (j + 1 == batch) {
                endNum = resList.size();
            }
            List<Map<String, Object>> subList = resList.subList(j * num, endNum);
            int i = 0;

            for (Map<String, Object> map : subList) {
                String fid = map.get("FID") + "";
                if (i != 0) {
                    sql.append("','");
                }
                sql.append(fid);
                i++;
            }
            sql.append("')");

            List data = null;
            try {
                data = dbQueryUtils.queryData(sql.toString());
            } catch (SQLException e) {
                logger.info(e.getMessage());
                errorRecord(e.getMessage(), "queryImageError", personTabName);
                return new ArrayList<>();
            }

            if (!XaUtil.isEmpty(data)) {
                for (Object s : data) {
                    Map<String, Object> obj = (Map<String, Object>) s;
                    String fid = obj.get("FID") + "";
                    String path = obj.get("PATH") + "";
                    pathMap.put(fid, path);
                }
            }
        }

        CountDownLatch countDownLatch = new CountDownLatch(resList.size());
        List<Map<String, Object>> errorList = new ArrayList<>();

        for (Map<String, Object> map : resList) {
            Map<String, Object> resMap = new HashMap<>();
            pollingData(map, resMap, new Date(), "ag_ry");

            JSONObject jsonObject = JSONObject.fromObject(resMap);
            String pushData = "";

            String fid = jsonObject.get("PERSON_SECURITY_CERTIFICATE_ID") + "";

            jsonObject.put("ID_CARDS_TYPE", getDicCode(jsonObject.get("ID_CARDS_TYPE") + "", "zj"));
            jsonObject.put("EDULEVEL", getDicCode(jsonObject.get("EDULEVEL") + "", "xl"));
            jsonObject.put("PERSON_TYPE", getDicCode(jsonObject.get("PERSON_TYPE") + "", "ry"));
            jsonObject.put("CERTIFICATE_AUTHORITY", "陕西省住房和城乡建设厅");
            jsonObject.put("CERTIFICATE_STATE", getDicCode(jsonObject.get("CERTIFICATE_STATE") + "", "zt"));

            String path = pathMap.get(fid);
            String image = "";
            if (!XaUtil.isEmpty(path)) {
                try {
                    image = ftpDownload(path);
                } catch (Exception e) {
                    logger.error(e.getMessage());
                    errorRecord("{'PERSON_SECURITY_CERTIFICATE':" + jsonObject.toString() + "}", "queryImageTableError", personTabName);
                    errorList.add(map);
                    countDownLatch.countDown();
                    continue;
                }
            }

            jsonObject.put("PHOTO", image);
            pushData = "{'PERSON_SECURITY_CERTIFICATE':" + jsonObject.toString() + "}";
            RYNUM += 1;
            logger.info(pushData);

            logger.info("接收数量：");
            logger.info(RYNUM + "");

            try {
                Thread.sleep(50);
                taskSchedulerManager.onlyOneTime(new PollingRunnable(fid, personTabName, pushData, RYNUM, countDownLatch, errorList, map, "ag_ry"));
            } catch (Exception e) {
                logger.error(e.getMessage());
            }
        }

        try {
            countDownLatch.await();
            if (!XaUtil.isEmpty(errorList)) {
                errorList.forEach(stringObjectMap -> {
                    resList.remove(stringObjectMap);
                });
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return resList;

    }

    //推送特种作业人员
    public List<Map<String, Object>> pushTz(String tableName, List<Map<String, Object>> resList) {

        if (XaUtil.isEmpty(resList)) {
            return new ArrayList<>();
        }

        try {
            FtpUtil.getInstance().connect(ftpClient, "192.168.2.180", 2121, "yjhh", "a1b2c3d4!");
        } catch (IOException e) {
            errorRecord(e.getMessage(), "ftpClientInitError", specialTabName);
            return new ArrayList<>();
        }

        Map<String, String> pathMap = new HashMap<>();

        int count = resList.size();
        int num = 1000;
        int batch = (count / num) + (count % num != 0 ? 1 : 0);
        for (int j = 0; j < batch; j++) {
            logger.info("关联图片查询批次");
            logger.info(j);
            StringBuilder sql = new StringBuilder();
            sql.append("SELECT  FLINKID as FID,FCONTENT as PATH from ");
            sql.append(imagesTabName);
            sql.append(" where FLINKID IN ( '");
            int endNum = (j + 1) * num;
            if (j + 1 == batch) {
                endNum = resList.size();
            }
            List<Map<String, Object>> subList = resList.subList(j * num, endNum);
            int i = 0;

            for (Map<String, Object> map : subList) {
                String fid = map.get("FID") + "";
                if (i != 0) {
                    sql.append("','");
                }
                sql.append(fid);
                i++;
            }
            sql.append("')");

            List data = null;
            try {
                data = dbQueryUtils.queryData(sql.toString());
            } catch (SQLException e) {
                logger.info(e.getMessage());
                errorRecord(e.getMessage(), "queryImageError", personTabName);
                return new ArrayList<>();
            }

            if (!XaUtil.isEmpty(data)) {
                for (Object s : data) {
                    Map<String, Object> obj = (Map<String, Object>) s;
                    String fid = obj.get("FID") + "";
                    String path = obj.get("PATH") + "";
                    pathMap.put(fid, path);
                }
            }
        }

        CountDownLatch countDownLatch = new CountDownLatch(resList.size());
        List<Map<String, Object>> errorList = new ArrayList<>();

        for (Map<String, Object> map : resList) {
            Map<String, Object> resMap = new HashMap<>();
            pollingData(map, resMap, new Date(), "tz_zy");

            JSONObject jsonObject = JSONObject.fromObject(resMap);
            String pushData = "";

            String fid = jsonObject.get("PERSON_OPERATION_CERTIFICATE_ID") + "";

            jsonObject.put("ID_CARDS_TYPE", getDicCode(jsonObject.get("ID_CARDS_TYPE") + "", "zj"));
            jsonObject.put("EDULEVEL", getDicCode(jsonObject.get("EDULEVEL") + "", "xl"));
            jsonObject.put("CERTIFICATE_AUTHORITY", "陕西省住房和城乡建设厅");
            jsonObject.put("CERTIFICATE_STATE", getDicCode(jsonObject.get("CERTIFICATE_STATE") + "", "tz_zt"));
            jsonObject.put("OPERATE_TYPE", getDicCode(jsonObject.get("OPERATE_TYPE") + "", "zzlb"));

            String path = pathMap.get(fid);
            String image = "";
            if (!XaUtil.isEmpty(path)) {
                try {
                    image = ftpDownload(path);
                } catch (Exception e) {
                    logger.error(e.getMessage());
                    errorRecord("{'PERSON_OPERATION_CERTIFICATE':" + jsonObject.toString() + "}", "queryImageTableError", personTabName);
                    errorList.add(map);
                    countDownLatch.countDown();
                    continue;
                }
            }

            jsonObject.put("PHOTO", image);
            pushData = "{'PERSON_OPERATION_CERTIFICATE':" + jsonObject.toString() + "}";
            RYNUM += 1;
            logger.info(pushData);

            logger.info("接收数量：");
            logger.info(RYNUM + "");

            try {
                Thread.sleep(50);
                taskSchedulerManager.onlyOneTime(new PollingRunnable(fid, specialTabName, pushData, RYNUM, countDownLatch, errorList, map, "tz_zy"));
            } catch (Exception e) {
                logger.error(e.getMessage());
            }
        }

        try {
            countDownLatch.await();
            if (!XaUtil.isEmpty(errorList)) {
                errorList.forEach(stringObjectMap -> {
                    resList.remove(stringObjectMap);
                });
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return resList;

    }


    //推送安许证
    public List<Map<String, Object>> pushZj(String tableName, List<Map<String, Object>> resList) {

        if (XaUtil.isEmpty(resList)) {
            return null;
        }

        CountDownLatch countDownLatch = new CountDownLatch(resList.size());
        List<Map<String, Object>> errorList = new ArrayList<>();
        for (Map<String, Object> map : resList) {
            Map<String, Object> resMap = new HashMap<>();
            pollingData(map, resMap, new Date(), "axz");
            JSONObject jsonObject = JSONObject.fromObject(resMap);
            String pushData = "";
            String fid = jsonObject.get("COMPANY_SAFETY_CERTIFICATE_ID") + "";
            String zszt = "无效".equals(jsonObject.get("CERTIFICATE_STATE") + "") ? "04" : "有效".equals(jsonObject.get("CERTIFICATE_STATE") + "") ? "01" : jsonObject.get("CERTIFICATE_STATE") + "";
            jsonObject.put("CERTIFICATE_STATE", zszt);
            pushData = "{'COMPANY_SAFETY_CERTIFICATE':" + jsonObject.toString() + "}";
            logger.info(pushData);
            ZJNUM += 1;
            logger.info("接收数量：");
            logger.info(ZJNUM + "");
            try {
                taskSchedulerManager.onlyOneTime(new PollingRunnableQyxy(fid, safetyTabName, pushData, ZJNUM, countDownLatch, errorList, map));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        try {
            countDownLatch.await();
            if (!XaUtil.isEmpty(errorList)) {
                errorList.forEach(stringObjectMap -> {
                    resList.remove(stringObjectMap);
                });
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        return resList;
    }

    //推送图片数据
    public List<Map<String, Object>> pushTp(String tableName, List<Map<String, Object>> resList) {

        List<Map<String, Object>> retList = new ArrayList<>();
        if (XaUtil.isEmpty(resList)) {
            return null;
        }
        try {
            FtpUtil.getInstance().connect(ftpClient, "192.168.2.180", 2121, "yjhh", "a1b2c3d4!");
        } catch (IOException e) {
            e.printStackTrace();
        }

        List<Map<String, Object>> errorList = new ArrayList<>();

        List<String> fTypeList = new ArrayList<>();
        fTypeList.add("ag_ry");
        fTypeList.add("tz_zy");

        for (String fType : fTypeList) {

            int count = resList.size();
            int num = 1000;
            int batch = (count / num) + (count % num != 0 ? 1 : 0);

            for (int j = 0; j < batch; j++) {
                logger.info("图片推送批次");
                logger.info(j);
                StringBuilder sql = new StringBuilder();
                sql.append("SELECT * from ");
                if ("ag_ry".equals(fType)) {
                    sql.append(personTabName);
                }
                if ("tz_zy".equals(fType)) {
                    sql.append(specialTabName);
                }
                sql.append(" where FID IN ( '");
                int endNum = (j + 1) * num;
                if (j + 1 == batch) {
                    endNum = resList.size();
                }
                List<Map<String, Object>> subList = resList.subList(j * num, endNum);
                int i = 0;
                Map<String, String> pathMap = new HashMap<>();
                Map<String, Map<String, Object>> imageMap = new HashMap<>();
                for (Map<String, Object> map : subList) {
                    String personId = map.get("FLINKID") + "";
                    String path = map.get("FCONTENT") + "";
                    if (i != 0) {
                        sql.append("','");
                    }
                    sql.append(personId);
                    pathMap.put(personId, path);
                    imageMap.put(personId, map);
                    i++;
                }
                sql.append("')");

                List data = null;
                try {
                    data = dbQueryUtils.queryData(sql.toString());
                } catch (SQLException e) {
                    logger.info(e.getMessage());
                    errorRecord(e.getMessage(), "queryTableError", imagesTabName);
                    errorList.addAll(subList);
                    continue;
                }

                if (!XaUtil.isEmpty(data)) {
                    CountDownLatch countDownLatch = new CountDownLatch(data.size());
                    for (Object s : data) {
                        Map<String, Object> obj = (Map<String, Object>) s;

                        String id = obj.get("FID") + "";
                        String image = "";
                        String path = pathMap.get(id);

                        Map<String, Object> resMap = new HashMap<>();
                        Map<String, Object> map = imageMap.get(id);

                        obj.remove("FTXT1");
                        obj.remove("FTXT2");
                        obj.remove("FTXT3");
                        obj.remove("FTXT4");
                        obj.remove("FTXT5");

                        pollingData(obj, resMap, new Date(), fType);
                        try {
                            image = ftpDownload(path);
                        } catch (Exception e) {
                            logger.info(e.getMessage());
                            errorRecord(e.getMessage(), "queryTableError", imagesTabName);
                            errorList.add(map);
                            countDownLatch.countDown();
                            continue;
                        }

                        if (!XaUtil.isEmpty(image)) {

                            resMap.put("PHOTO", image);
                            resMap.put("OPERATION_FLAG", "U");
                            resMap.put("PHOTO", image);

                            resMap.put("ID_CARDS_TYPE", getDicCode(resMap.get("ID_CARDS_TYPE") + "", "zj"));
                            resMap.put("EDULEVEL", getDicCode(resMap.get("EDULEVEL") + "", "xl"));
                            resMap.put("CERTIFICATE_AUTHORITY", "陕西省住房和城乡建设厅");

                            String pushData = "";

                            if ("ag_ry".equals(fType)) {
                                resMap.put("CERTIFICATE_STATE", getDicCode(resMap.get("CERTIFICATE_STATE") + "", "zt"));
                                resMap.put("PERSON_TYPE", getDicCode(resMap.get("PERSON_TYPE") + "", "ry"));
                                pushData = "{'PERSON_SECURITY_CERTIFICATE':" + net.sf.json.JSONObject.fromObject(resMap).toString() + "}";
                            }
                            if ("tz_zy".equals(fType)) {
                                resMap.put("OPERATE_TYPE", getDicCode(resMap.get("OPERATE_TYPE") + "", "zzlb"));
                                resMap.put("CERTIFICATE_STATE", getDicCode(resMap.get("CERTIFICATE_STATE") + "", "tz_zt"));
                                pushData = "{'PERSON_OPERATION_CERTIFICATE':" + net.sf.json.JSONObject.fromObject(resMap).toString() + "}";
                            }

                            TPNUM += 1;
                            logger.info("接收数量：");
                            logger.info(TPNUM + "");

                            retList.add(map);
                            try {
                                taskSchedulerManager.onlyOneTime(new PollingRunnable(id, imagesTabName, pushData, TPNUM, countDownLatch, errorList, map, fType));
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        } else {
                            countDownLatch.countDown();
                            continue;
                        }
                    }
                    try {
                        countDownLatch.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }

        if (!XaUtil.isEmpty(errorList)) {
            errorList.forEach(stringObjectMap -> {
                retList.remove(stringObjectMap);
            });
        }

        return retList;
    }

    //通过ftp下载图片并转为Base64字符串
    private String ftpDownload(String path) {


        String imgBase64 = "";
        try {
            byte[] imageByte = FtpUtil.getInstance().download(ftpClient, path);
            if (!XaUtil.isEmpty(imageByte)) {
                imgBase64 = Base64.encodeBase64String(imageByte);
                return imgBase64;
            } else {
                return imgBase64;
            }

        } catch (IOException e) {
            e.printStackTrace();
            return imgBase64;
        }
    }

    private String getDicCode(String s, String type) {
        String dicCode = "";

        if ("ry".equals(type)) {
            if ("C证人员".equals(s)) {
                dicCode = "03";
            }
            if ("C3证人员".equals(s)) {
                dicCode = "06";
            }
            if ("C2证人员".equals(s)) {
                dicCode = "05";
            }
            if ("C1证人员".equals(s)) {
                dicCode = "04";
            }
            if ("B证人员".equals(s)) {
                dicCode = "02";
            }
            if ("A证人员".equals(s)) {
                dicCode = "01";
            }

        }

        if ("zj".equals(type)) {
            if ("身份证".equals(s)) {
                dicCode = "01";
            }
            if ("军官证".equals(s)) {
                dicCode = "02";
            }
            if ("其他".equals(s)) {
                dicCode = "99";
            }
        }
        if ("zt".equals(type)) {
            if ("2".equals(s)) {
                dicCode = "01";
            }
            if ("-1".equals(s)) {
                dicCode = "04";
            }
        }
        if ("tz_zt".equals(type)) {
            if ("1".equals(s)) {
                dicCode = "01";
            }
            if ("-1".equals(s)) {
                dicCode = "04";
            }
        }

        if ("zzlb".equals(type)) {
            if ("建筑电工".equals(s)) {
                dicCode = "01";
            } else if (s.contains("建筑架子工")) {
                dicCode = "02";
            } else if (s.contains("司索")) {
                dicCode = "03";
            } else if (s.contains("司机")) {
                dicCode = "04";
            } else if ("高处作业吊蓝安装拆卸工".equals(s) || "高处作业吊篮安装拆卸工".equals(s)) {
                dicCode = "06";
            } else if (s.contains("安装拆卸工")) {
                dicCode = "05";
            } else {
                dicCode = "07";
            }
        }

        if ("xl".equals(type)) {
            if ("博士".equals(s) || "博士后".equals(s)) {
                dicCode = "01";
            }
            if ("无".equals(s)) {
                dicCode = "99";
            }

            if ("大专".equals(s)) {
                dicCode = "04";
            }
            if ("中专".equals(s)) {
                dicCode = "05";
            }
            if ("本科".equals(s)) {
                dicCode = "03";
            }
            if ("中专以下(含高中初中)".equals(s)) {
                dicCode = "06";
            }
            if ("硕士".equals(s)) {
                dicCode = "02";
            }
        }
        return dicCode;
    }


    private void pollingData(Map<String, Object> map, Map<String, Object> resMap, Date date, String fType) {

        dicMap = getDicMap();

        if ("axz".equals(fType)) {

            resMap.put("COMPANY_SAFETY_CERTIFICATE_ID", map.get("FID") + "");
            resMap.put("PROVICE_CODE", "610000");
            resMap.put("CERTIFICATE_NUMBER", map.get("FCERTINO") + "");
            resMap.put("COMPANY_NAME", map.get("FENTNAME") + "");
            resMap.put("CREDIT_CODE", map.get("FENTLICENCE") + "");
            resMap.put("RESPONSIBLE_PERSON_NAME", map.get("FENTJURIDICAL") + "");
            resMap.put("COMPANY_ADDRESS", map.get("FENTADDRESS") + "");
            resMap.put("ECONOMY_TYPE", !XaUtil.isEmpty(map.get("FENTTYPEID")) ? dicMap.get(map.get("FENTTYPEID")) : "");
            resMap.put("LICENSE_RANGE", map.get("FALLOWRANGE") + "");
            resMap.put("CERTIFICATE_START_DATE", dateFormat(map.get("FBEGINTIME")) + "");
            resMap.put("CERTIFICATE_END_DATE", dateFormat(map.get("FENDTIME")) + "");
            resMap.put("CERTIFICATE_AUTHORITY", "陕西省住房和城乡建设厅");
            resMap.put("CERTIFICATE_SEND_DATE", dateFormat(map.get("FAPPTIME")));
            resMap.put("CERTIFICATE_LATE_AUTHORITY", "");
            resMap.put("CERTIFICATE_LATE_DATE", "");
            resMap.put("CERTIFICATE_STATE", "0".equals(map.get("FISVALID")) ? "无效" : "1".equals(map.get("FISVALID")) ? "有效" : "");
            resMap.put("ELECTRONIC_CERTIFICATES", "");
            resMap.put("OPERATION_FLAG", map.get("OPERATION_FLAG") + "");
            resMap.put("CREATEDATE", CommonUtils.YYYYMMDD.format(date));
        }

        if ("ag_ry".equals(fType)) {

            String sex = map.get("FSEX") + "";
            if (!StringUtils.isBlank(sex)) {
                sex = "男".equals(sex.trim()) ? "01" : "女".equals(sex.trim()) ? "02" : "";
            }
            resMap.put("PERSON_SECURITY_CERTIFICATE_ID", map.get("FID"));
            resMap.put("PROVICE_CODE", "610000");

            resMap.put("NAME", map.get("FNAME"));
            resMap.put("ID_CARDS_TYPE", !XaUtil.isEmpty(map.get("FIDCARDTYPEID")) ? dicMap.get(map.get("FIDCARDTYPEID")) : "");
            resMap.put("ID_CARDS_NUMBER", map.get("FIDCARD"));
            resMap.put("SEX", sex);
            resMap.put("BIRTHDAY", dateFormat(map.get("FBIRTHDAY")));
            resMap.put("EDULEVEL", !XaUtil.isEmpty(map.get("FDEGREEID")) ? dicMap.get(map.get("FDEGREEID")) : "");
            resMap.put("PHOTO", "");
            resMap.put("PERSON_TYPE", !XaUtil.isEmpty(map.get("FPERSONTYPEID")) ? dicMap.get(map.get("FPERSONTYPEID")) : "");
            resMap.put("COMPANY_NAME", map.get("FBASENAME"));
            resMap.put("COMPANY_CREDIT_CODE", map.get("FJURIDCIALCODE"));
            resMap.put("POST", map.get("FFUNCTION"));
            resMap.put("POSITIONAL_TITLE", "");
            resMap.put("CERTIFICATE_NUMBER", map.get("FCERTINO"));
            resMap.put("CERTIFICATE_START_DATE", dateFormat(map.get("FBEGINTIME")));
            resMap.put("CERTIFICATE_END_DATE", dateFormat(map.get("FENDTIME")));
            resMap.put("CERTIFICATE_AUTHORITY", "陕西省住房和城乡建设厅");
            resMap.put("CERTIFICATE_SEND_DATE", dateFormat(map.get("FAPPTIME")));
            resMap.put("CERTIFICATE_CONTINUE_DATE", "");
            resMap.put("CERTIFICATE_STATE", map.get("FSTATE"));
            resMap.put("ELECTRONIC_CERTIFICATES", "");
            resMap.put("OPERATION_FLAG", map.get("OPERATION_FLAG"));
            resMap.put("CREATEDATE", CommonUtils.YYYYMMDD.format(date));
            resMap.put("CERTIFICATE_RECHECK_DATE", "");
            resMap.put("OPERATE_TYPE", "");
        }
        if ("tz_zy".equals(fType)) {

            String sex = map.get("FSEX") + "";
            if (!StringUtils.isBlank(sex)) {
                sex = "男".equals(sex.trim()) ? "01" : "女".equals(sex.trim()) ? "02" : "";
            }
            resMap.put("PERSON_OPERATION_CERTIFICATE_ID", map.get("FID"));
            resMap.put("PROVICE_CODE", "610000");

            resMap.put("NAME", map.get("FNAME"));
            resMap.put("ID_CARDS_TYPE", !XaUtil.isEmpty(map.get("FIDCARDTYPEID")) ? dicMap.get(map.get("FIDCARDTYPEID")) : "身份证");
            resMap.put("ID_CARDS_NUMBER", map.get("FIDCARD"));
            resMap.put("SEX", sex);
            resMap.put("BIRTHDAY", dateFormat(map.get("FBIRTHDAY")));
            resMap.put("EDULEVEL", !XaUtil.isEmpty(map.get("FDEGREEID")) ? dicMap.get(map.get("FDEGREEID")) : "");
            resMap.put("PHOTO", "");
            resMap.put("CERTIFICATE_NUMBER", map.get("FCERTINO"));
            resMap.put("CERTIFICATE_START_DATE", dateFormat(map.get("FBEGINTIME")));
            resMap.put("CERTIFICATE_END_DATE", dateFormat(map.get("FENDTIME")));
            resMap.put("CERTIFICATE_AUTHORITY", "陕西省住房和城乡建设厅");
            resMap.put("CERTIFICATE_SEND_DATE", dateFormat(map.get("FAPPTIME")));
            resMap.put("CERTIFICATE_STATE", map.get("FSTATE"));
            resMap.put("ELECTRONIC_CERTIFICATES", "");
            resMap.put("OPERATION_FLAG", map.get("OPERATION_FLAG"));
            resMap.put("CERTIFICATE_RECHECK_DATE", map.get("FGRADUATETIME"));
            String fBaseName = !StringUtils.isBlank(map.get("FPERSONTYPEID") + "") ? dicMap.get(map.get("FPERSONTYPEID")+"") :
                    map.get("FBASENAME") + "";
            resMap.put("OPERATE_TYPE", fBaseName);
        }

    }

    private String dateFormat(Object time) {
        if (XaUtil.isEmpty(time) || "null".equals(time + "")) {
            return null;
        }
        String dateFormat = time + "";
        return dateFormat.substring(0, 10);
    }

    private void errorRecord(String json, String resCode, String tableName) {
        String url = "";
        ConstConfig.DBType dbType = ConstConfig.DBType.valueOf(Integer.valueOf(recordDbSType));
        if ("3".equals(recordDbSType)) {
            url = "jdbc:mysql://" + recordIp + ":" + recordPort + "/" + recordDbName + "?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useInformationSchema=true&useSSL=false";
        } else {
            url = DBInfoUtil.getDBUrl(dbType, recordIp, recordPort, recordDbName);
        }

        DBQueryUtils dbQueryUtils = new DBQueryUtils(DBInfoUtil.getDBDriverName(dbType),
                url, recordUserName, recordPassword, 5);

        List<Map<String, Object>> dataList = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        map.put("id", IdGenerator.createNewId());
        map.put("task_id", "");
        map.put("time", CommonUtils.YYYYMMDDHHMMSS.format(new Date()));
        map.put("error_message", resCode);
        map.put("error_data", json);
        map.put("table_name", tableName);
        dataList.add(map);
        try {
            dbQueryUtils.insertBatchData("data_exchange_error", recordDbName, dataList);
        } catch (SQLException e) {
            e.printStackTrace();
        }

    }

}
